package com.inei.evaluacionemype.controller.activity;

import com.inei.evaluacionemype.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class CapituloIV_ParteII_Activity extends Activity {

	// componentes
	private EditText etC4_5, etC4_7, etC4_9, etC4_11_O, etC4_a, etC4_g;
	private RadioButton rbC4_51, rbC4_52, rbC4_61, rbC4_62, rbC4_81, rbC4_82,
			rbC4_101, rbC4_102;
	private CheckBox cbC4_11_11, cbC4_11_12, cbC4_11_13, cbC4_11_14,
			cbC4_11_21, cbC4_11_22, cbC4_11_23, cbC4_11_O, cbC4_a1, cbC4_a2,
			cbC4_a3, cbC4_a4, cbC4_b1, cbC4_c1, cbC4_d1, cbC4_e1, cbC4_f1,
			cbC4_g1;

	// variables

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_capitulo_iv_parte_2);

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.capitulo_iv_2, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnGuardar:
			String resultado = Validar();
			// if (resultado.equals("")) {
			if (true) {
				GuardarDatos();
			} else {
				Toast.makeText(this, resultado, Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.mnSalir:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_cap4_1 = new Intent(this, CapituloIV_ParteI_Activity.class);
		ir_cap4_1.putExtras(getIntent().getExtras());
		startActivity(ir_cap4_1);
		finish();
		super.onBackPressed();
	}

	private void IniciarComponentes() {
		etC4_5 = (EditText) findViewById(R.id.etC4_5);
		etC4_7 = (EditText) findViewById(R.id.etC4_7);
		etC4_9 = (EditText) findViewById(R.id.etC4_9);
		etC4_11_O = (EditText) findViewById(R.id.etC4_11_O);
		etC4_a = (EditText) findViewById(R.id.etC4_a);
		etC4_g = (EditText) findViewById(R.id.etC4_g);

		rbC4_51 = (RadioButton) findViewById(R.id.rbC4_51);
		rbC4_52 = (RadioButton) findViewById(R.id.rbC4_52);
		rbC4_61 = (RadioButton) findViewById(R.id.rbC4_61);
		rbC4_62 = (RadioButton) findViewById(R.id.rbC4_62);
		rbC4_81 = (RadioButton) findViewById(R.id.rbC4_81);
		rbC4_82 = (RadioButton) findViewById(R.id.rbC4_82);
		rbC4_101 = (RadioButton) findViewById(R.id.rbC4_101);
		rbC4_102 = (RadioButton) findViewById(R.id.rbC4_102);

		cbC4_11_11 = (CheckBox) findViewById(R.id.cbC4_11_11);
		cbC4_11_12 = (CheckBox) findViewById(R.id.cbC4_11_12);
		cbC4_11_13 = (CheckBox) findViewById(R.id.cbC4_11_13);
		cbC4_11_14 = (CheckBox) findViewById(R.id.cbC4_11_14);
		cbC4_11_21 = (CheckBox) findViewById(R.id.cbC4_11_21);
		cbC4_11_22 = (CheckBox) findViewById(R.id.cbC4_11_22);
		cbC4_11_23 = (CheckBox) findViewById(R.id.cbC4_11_23);
		cbC4_11_O = (CheckBox) findViewById(R.id.cbC4_11_O);
		cbC4_a1 = (CheckBox) findViewById(R.id.cbC4_a1);
		cbC4_a2 = (CheckBox) findViewById(R.id.cbC4_a2);
		cbC4_a3 = (CheckBox) findViewById(R.id.cbC4_a3);
		cbC4_a4 = (CheckBox) findViewById(R.id.cbC4_a4);
		cbC4_b1 = (CheckBox) findViewById(R.id.cbC4_b1);
		cbC4_c1 = (CheckBox) findViewById(R.id.cbC4_c1);
		cbC4_d1 = (CheckBox) findViewById(R.id.cbC4_d1);
		cbC4_e1 = (CheckBox) findViewById(R.id.cbC4_e1);
		cbC4_f1 = (CheckBox) findViewById(R.id.cbC4_f1);
		cbC4_g1 = (CheckBox) findViewById(R.id.cbC4_g1);

		etC4_7.setEnabled(false);
		etC4_9.setEnabled(false);
		etC4_11_O.setEnabled(false);
		etC4_a.setEnabled(false);
		etC4_g.setEnabled(false);
	}

	private void AgregarEventos() {
		EventoSiNo(rbC4_61);
		EventoSiNo(rbC4_62);
		EventoSiNo(rbC4_81);
		EventoSiNo(rbC4_82);
		EventoOtro(cbC4_11_O);
		EventoOtro(cbC4_a4);
		EventoOtro(cbC4_g1);
	}

	private void EventoSiNo(final RadioButton rb) {
		rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (rb == rbC4_61 || rb == rbC4_62) {
					etC4_7.requestFocus();
					etC4_7.setEnabled(rbC4_61.isChecked() ? true : false);
					etC4_7.setText(rbC4_62.isChecked() ? "" : null);
				}
				if (rb == rbC4_81 || rb == rbC4_82) {
					etC4_9.requestFocus();
					etC4_9.setEnabled(rbC4_81.isChecked() ? true : false);
					etC4_9.setText(rbC4_82.isChecked() ? "" : null);
				}
			}
		});
	}

	private void EventoOtro(final CheckBox cb) {
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (cb == cbC4_11_O) {
					etC4_11_O.requestFocus();
					etC4_11_O.setEnabled(cbC4_11_O.isChecked() ? true : false);
					etC4_11_O.setText(cbC4_11_O.isChecked() ? null : "");
				}
				if (cb == cbC4_a4) {
					etC4_a.requestFocus();
					etC4_a.setEnabled(cbC4_a4.isChecked() ? true : false);
					etC4_a.setText(cbC4_a4.isChecked() ? null : "");
				}
				if (cb == cbC4_g1) {
					etC4_g.requestFocus();
					etC4_g.setEnabled(cbC4_g1.isChecked() ? true : false);
					etC4_g.setText(cbC4_g1.isChecked() ? null : "");
				}
			}
		});
	}

	private void CargarDatos() {

	}

	private void GuardarDatos() {
		Intent ir_cap4_3 = new Intent(this, CapituloIV_ParteIII_Activity.class);
		startActivity(ir_cap4_3);
		finish();
	}

	private String Validar() {
		return "";
	}
}
